﻿using System;
using System.Collections.Generic;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.Service.First;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage;

namespace FoodRaidersWebAPI.Controllers
{
    [ApiController]
    [Route("[controller]/api")]
    public class RaiderArticleController : Controller
    {
        private readonly FoodContext context;
        private readonly RaiderArticleService raiderArticleService;
        private readonly UserService userService;
        
        public RaiderArticleController(FoodContext dbContext)
        {
            this.context = dbContext;
            raiderArticleService = new RaiderArticleService(context);
            userService = new UserService(context);
        }
        //api 1
        [HttpGet("ListByPraise/n={n}")]
        public ActionResult<IEnumerable<RaiderArticle>> ListByPraise(int n)
        {
            return raiderArticleService.ListByPraise(n);
        }
        //api 2
        [HttpGet("ListByDateTime/n={n}")]
        public ActionResult<IEnumerable<RaiderArticle>> ListByDateTime(int n)
        {
            return raiderArticleService.ListByDateTime(n);
        }
        //api 3
        [HttpGet("findAllLoad/id={id}")]
        public ActionResult<RaiderArticle> findAllLoad(int id)
        {
            return raiderArticleService.findByIDFullLoad(id);
        }
        //api 4
        [HttpGet("addPraise/id={id}")]
        public void addPraise(int id)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                raiderArticleService.addPraise(id);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 5
        [HttpGet("subPraise/id={id}")]
        public void subPraise(int id)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                raiderArticleService.subPraise(id);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 6
        [HttpGet("user/id={id}")]
        public ActionResult<User> user(int id)
        {
            int uid = raiderArticleService.authorID(id);
            return userService.findByID(uid);
        }
        //api 7
        [HttpGet("addComment/id={id}&content={content}&uid={uid}")]
        public void addComment(int id, string content, int uid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                raiderArticleService.addComment(id,uid,content);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 8
        [HttpGet("delete/id={id}")]
        public ActionResult<RaiderArticle> delete(int id)
        {
            RaiderArticle raiderArticle;
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                raiderArticle = raiderArticleService.delete(id);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }

            return raiderArticle;
        }
        
        //api 1
        [HttpPost("addRaiderArticle")]
        public void addRaiderArticle(RaiderArticle raiderArticle)
        {
            if (raiderArticle.Raider == null || raiderArticle.Article == null)
            {
                throw new Exception("缺少Merchant或Article");
            }
            raiderArticle.Article.DateTime=DateTime.Now;
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                raiderArticleService.addRaiderArticle(raiderArticle);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
    }
}